﻿<?php

class lists
{
	public $type;
	public $pid;
	public $el_name;
	public $sel_value;	

	public function processor()
	{
		switch($this->type) {
			case "select":
				return self::createSelect();
			break;
			
			case "checkbox":
				return self::createCheckbox();
			break;
			
			case "radio":
			default:
				return self::createRadio();
			break;
		}
	}
	
	private function createRadio() {
		$r =& Registry::get("MDB")->query("SELECT `Name`,`ID` FROM `".PRFX."lists` WHERE `Show`='1' AND `Parent`='".$this->pid."' ORDER BY `Por`;");		
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}
		if ($r->numRows()) {
			while($data = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$out.= '<label for="radio_input'.$this->el_name.$data["id"].'"><input id="radio_input'.$this->el_name.$data["id"].'" type="radio" name="'.$this->el_name.'" value="'.$data["id"].'"'.($this->sel_value == $data["id"] ? ' checked="checked"' : '').' /> '.$data["name"].'</label> ';
			}			
			return $out;
		}
	}
	
	private function createCheckbox() {
		$r =& Registry::get("MDB")->query("SELECT `Name`,`ID` FROM `".PRFX."lists` WHERE `Show`='1' AND `Parent`='".$this->pid."' ORDER BY `Por`;");		
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}
		if ($r->numRows()) {
			while($data = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$out.= '<label for="checkbox_input'.$this->el_name.$data["id"].'"><input id="checkbox_input'.$this->el_name.$data["id"].'" type="checkbox" name="'.$this->el_name.'[]" value="'.$data["id"].'"'.(in_array($data["id"], $this->sel_value) ? ' checked="checked"' : '').' /> '.$data["name"].'</label> ';
			}			
			return $out;
		}
	}
		
	private function createSelect() {
		$r =& Registry::get("MDB")->query("SELECT `Name`,`Value` FROM `".PRFX."lists` WHERE `Show`='1' AND `Parent`='".$this->pid."' ORDER BY `Por`;");		
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}
		if ($r->numRows()) 
		{
			while($data = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
				$out.= '<option value="'.$data["value"].'"'.($this->sel_value == $data["value"] ? ' selected="selected"' : '').'>'.$data["name"].'</option>';
			}
			
			return '<select name="'.$this->el_name.'">'.($this->none_item ? '<option value="0">Ничего не выбрано</option>' : '').$out.'</select>';
		}
	}
}

?>